package com.quectel.mobile.controller.household;

import cn.hutool.core.text.CharSequenceUtil;
import com.quectel.core.module.household.dto.HouseholdAuditDto;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdRoomDto;
import com.quectel.core.module.household.service.HouseholdRoomService;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.property.dto.PropertyInfoDto;
import com.quectel.core.module.property.service.PropertyInfoService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.mobile.vo.householdroom.QueryPropertyResp;
import com.quectel.mobile.vo.householdroom.RemoveHouseholdVo;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author rananxin
 * @date 2021/12/23 4:51 下午
 */
@RestController
@RequestMapping("householdRoom")
@Tag(name = "HouseholdRoomController", description = "人员房间相关api")
public class HouseholdRoomController extends BaseController {

    @DubboReference
    private HouseholdRoomService householdRoomService;
    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private PropertyInfoService propertyInfoService;


    @PostMapping("removeHousehold")
    @Operation(summary = "移除住户")
    public Response<Object> removeHousehold(@RequestBody RemoveHouseholdVo removeHouseholdVo) {
        HouseholdDto currentHousehold = SessionHolder.getLoginMobileUserSession().getCurrentHousehold();
        if (currentHousehold == null) {
            return Response.error("未认证为住户");
        }
        HouseholdRoomDto householdRoomDto = householdRoomService.selectByHouseholdIdAndRoomId(currentHousehold.getId(), removeHouseholdVo.getRoomId());
        if (!HouseholdAuditDto.IdentityCategoryEnum.OWNER.getType().equals(householdRoomDto.getIdentityCategory())) {
            return Response.error("不是该房间的业主，不能移除他人");
        }
        HouseholdRoomDto removeHousehold = householdRoomService.selectByHouseholdIdAndRoomId(removeHouseholdVo.getHouseholdId(), removeHouseholdVo.getRoomId());
        if (HouseholdAuditDto.IdentityCategoryEnum.OWNER.getType().equals(removeHousehold.getIdentityCategory())) {
            return Response.error("不能移除业主");
        }
        householdRoomService.deleteByHouseholdIdAndRoomId(removeHouseholdVo.getRoomId(), removeHouseholdVo.getHouseholdId());
        return Response.ok();
    }

    @GetMapping("queryProperty")
    @Operation(summary = "查询用户资产列表")
    public Response<List<QueryPropertyResp>> queryProperty() {
        SessionHolder.checkMobileUser();

        MobileUserDto mobileUser = SessionHolder.getLoginMobileUserSession().getMobileUser();
        String mobile = mobileUser.getMobile();
        List<HouseholdDto> householdDtos = householdService.selectByMobile(mobile);

        List<QueryPropertyResp> rooms = new ArrayList<>();
        for (HouseholdDto householdDto : householdDtos) {
            List<HouseholdRoomDto> householdRoomDtos = householdRoomService.selectByHouseholdId(householdDto.getId());
            for (HouseholdRoomDto householdRoomDto : householdRoomDtos) {
                QueryPropertyResp resp = new QueryPropertyResp();
                resp.setVillageRoom(householdRoomDto.getVillageRoomDto());
                resp.setIdentityCategory(householdRoomDto.getIdentityCategory());
                rooms.add(resp);
            }
        }

        for (QueryPropertyResp resp : rooms) {
            PropertyInfoDto propertyInfoDto = propertyInfoService.selectCacheByVillageId(resp.getVillageRoom().getVillageId());
            if (propertyInfoDto != null) {
                resp.setPropertyName(propertyInfoDto.getName());
            }
        }

        return Response.<List<QueryPropertyResp>>ok().wrap(rooms);
    }


    @GetMapping("getRooms")
    @Operation(summary = "获取住户房屋列表")
    public Response<List<HouseholdRoomDto>> getRooms(
            @Parameter(description = "认证身份筛选,列表用逗号隔开'1,2',字典值来源:HouseholdIdentityCategory") @RequestParam(required = false) String identityCategories
    ) {
        SessionHolder.checkHousehold();

        HouseholdDto currentHousehold = SessionHolder.getLoginMobileUserSession().getCurrentHousehold();
        if (currentHousehold == null) {
            return Response.error("未认证为住户");
        }

        List<HouseholdRoomDto> householdRoomDtos = householdRoomService.selectByHouseholdId(currentHousehold.getId());

        if (StringUtils.isNotBlank(identityCategories)) {
            List<String> identityCategoryList = CharSequenceUtil.splitTrim(identityCategories, ",");
            if (CollectionUtils.isNotEmpty(identityCategoryList)) {
                return Response.<List<HouseholdRoomDto>>ok().wrap(
                        householdRoomDtos.stream()
                                .filter(householdRoomDto -> identityCategoryList.contains(String.valueOf(householdRoomDto.getIdentityCategory())))
                                .collect(Collectors.toList())
                );
            }
        }

        return Response.<List<HouseholdRoomDto>>ok().wrap(householdRoomDtos);
    }

    @GetMapping("getHouseholds")
    @Operation(summary = "获取房屋绑定的住户列表")
    public Response<List<HouseholdRoomDto>> getHouseholds(
            @Parameter(description = "房间id") @RequestParam Long roomId) {

        HouseholdDto currentHousehold = SessionHolder.getLoginMobileUserSession().getCurrentHousehold();
        if (currentHousehold == null) {
            return Response.error("未认证为住户");
        }

        List<HouseholdRoomDto> list = householdRoomService.selectByRoomId(roomId);
        return Response.<List<HouseholdRoomDto>>ok().wrap(list);
    }

}

